﻿using System;
using System.Collections.Generic;

namespace Lottery.App
{
    public class Utils
    {
        public const string FORMAT_N1 = "{0:D2}";
        public const string FORMAT_N2 = "{0:D2} {1:D2}";
        public const string FORMAT_N3 = "{0:D2} {1:D2} {2:D2}";
        public const string FORMAT_N4 = "{0:D2} {1:D2} {2:D2} {3:D2}";
        public const string FORMAT_N5 = "{0:D2} {1:D2} {2:D2} {3:D2} {4:D2}";
        public const string FORMAT_N6 = "{0:D2} {1:D2} {2:D2} {3:D2} {4:D2} {5:D2}";
        public const string FORMAT_N7 = "{0:D2} {1:D2} {2:D2} {3:D2} {4:D2} {5:D2} {6:D2}";
        public const string FORMAT_N8 = "{0:D2} {1:D2} {2:D2} {3:D2} {4:D2} {5:D2} {6:D2} {7:D2}";

        /// <summary>
        /// 6选5(6种组合)
        /// </summary>
        public static List<string> Get6P5(string n6)
        {
            string[] array = n6.Split(' ');

            List<string> list = new List<string>(6);

            for (int i = 0; i < 2; i++)
            {
                for (int j = i + 1; j < 3; j++)
                {
                    for (int k = j + 1; k < 4; k++)
                    {
                        for (int m = k + 1; m < 5; m++)
                        {
                            for (int n = m + 1; n < 6; n++)
                            {
                                list.Add(String.Format(FORMAT_N5, array[i], array[j], array[k], array[m], array[n]));
                            }
                        }
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 7选5(21种组合)
        /// </summary>
        public static List<string> Get7P5(string n7)
        {
            string[] array = n7.Split(' ');

            List<string> list = new List<string>(21);

            for (int i = 0; i < 3; i++)
            {
                for (int j = i + 1; j < 4; j++)
                {
                    for (int k = j + 1; k < 5; k++)
                    {
                        for (int m = k + 1; m < 6; m++)
                        {
                            for (int n = m + 1; n < 7; n++)
                            {
                                list.Add(String.Format(FORMAT_N5, array[i], array[j], array[k], array[m], array[n]));
                            }
                        }
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 8选5(56种组合)
        /// </summary>
        public static List<string> Get8P5(string n8)
        {
            string[] items = n8.Split(' ');

            List<string> list = new List<string>(56);

            for (int i = 0; i <= 3; i++)
            {
                for (int j = i + 1; j <= 4; j++)
                {
                    for (int k = j + 1; k <= 5; k++)
                    {
                        for (int m = k + 1; m <= 6; m++)
                        {
                            for (int n = m + 1; n <= 7; n++)
                            {
                                list.Add(String.Format(FORMAT_N5, items[i], items[j], items[k], items[m], items[n]));
                            }
                        }
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 11选1(1种组合)
        /// </summary>
        public static List<string> Get11P1()
        {
            List<string> list = new List<string>(1);

            for (int i = 1; i <= 11; i++)
            {
                list.Add(String.Format(FORMAT_N1, i));
            }

            return list;
        }

        /// <summary>
        /// 11选2(55种组合)
        /// </summary>
        public static List<string> Get11P2()
        {
            List<string> list = new List<string>(55);

            for (int i = 1; i <= 10; i++)
            {
                for (int j = i + 1; j <= 11; j++)
                {
                    list.Add(String.Format(FORMAT_N2, i, j));
                }
            }

            return list;
        }

        /// <summary>
        /// 11选3(165种组合)
        /// </summary>
        public static List<string> Get11P3()
        {
            List<string> list = new List<string>(165);

            for (int i = 1; i <= 9; i++)
            {
                for (int j = i + 1; j <= 10; j++)
                {
                    for (int k = j + 1; k <= 11; k++)
                    {
                        list.Add(String.Format(FORMAT_N3, i, j, k));
                    }
                }
            }
            
            return list;
        }

        /// <summary>
        /// 11选4(330种组合)
        /// </summary>
        public static List<string> Get11P4()
        {
            List<string> list = new List<string>(330);

            for (int i = 1; i <= 8; i++)
            {
                for (int j = i + 1; j <= 9; j++)
                {
                    for (int k = j + 1; k <= 10; k++)
                    {
                        for (int m = k + 1; m <= 11; m++)
                        {
                            list.Add(String.Format(FORMAT_N4, i, j, k, m));
                        }
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 11选5(462种组合)
        /// </summary>
        /// <returns></returns>
        public static List<string> Get11P5()
        {
            List<string> list = new List<string>(462);
            
            for (int i = 1; i <= 7; i++)
            {
                for (int j = i + 1; j <= 8; j++)
                {
                    for (int k = j + 1; k <= 9; k++)
                    {
                        for (int m = k + 1; m <= 10; m++)
                        {
                            for (int n = m + 1; n <= 11; n++)
                            {
                                list.Add(String.Format(FORMAT_N5, i, j, k, m, n));
                            }
                        }
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 11选6(462种组合)
        /// </summary>
        /// <returns></returns>
        public static List<string> Get11P6()
        {
            List<string> list = new List<string>(462);

            for (int i = 1; i <= 6; i++)
            {
                for (int j = i + 1; j <= 7; j++)
                {
                    for (int k = j + 1; k <= 8; k++)
                    {
                        for (int m = k + 1; m <= 9; m++)
                        {
                            for (int n = m + 1; n <= 10; n++)
                            {
                                for (int x = n + 1; x <= 11; x++)
                                {
                                    list.Add(String.Format(FORMAT_N6, i, j, k, m, n, x));
                                }
                            }
                        }
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 11选7(330种组合)
        /// </summary>
        /// <returns></returns>
        public static List<string> Get11P7()
        {
            List<string> list = new List<string>(330);

            for (int i = 1; i <= 5; i++)
            {
                for (int j = i + 1; j <= 6; j++)
                {
                    for (int k = j + 1; k <= 7; k++)
                    {
                        for (int m = k + 1; m <= 8; m++)
                        {
                            for (int n = m + 1; n <= 9; n++)
                            {
                                for (int x = n + 1; x <= 10; x++)
                                {
                                    for (int y = x + 1; y <= 11; y++)
                                    {
                                        list.Add(String.Format(FORMAT_N7, i, j, k, m, n, x, y));
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 11选8(165种组合)
        /// </summary>
        /// <returns></returns>
        public static List<string> Get11P8()
        {
            List<string> list = new List<string>(165);

            for (int i = 1; i <= 4; i++)
            {
                for (int j = i + 1; j <= 5; j++)
                {
                    for (int k = j + 1; k <= 6; k++)
                    {
                        for (int m = k + 1; m <= 7; m++)
                        {
                            for (int n = m + 1; n <= 8; n++)
                            {
                                for (int x = n + 1; x <= 9; x++)
                                {
                                    for (int y = x + 1; y <= 10; y++)
                                    {
                                        for (int z = y + 1; z <= 11; z++)
                                        {
                                            list.Add(String.Format(FORMAT_N8, i, j, k, m, n, x, y, z));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 11排2(110种组合)
        /// </summary>
        public static List<string> Get11A2()
        {
            string[] array = new string[110];
            int index = 0;
            for (int i = 1; i <= 10; i++)
            {
                for (int j = i + 1; j <= 11; j++)
                {
                    array[index++] = String.Format(FORMAT_N2, i, j);
                    array[index++] = String.Format(FORMAT_N2, j, i);
                }
            }
            Array.Sort(array);

            List<string> list = new List<string>(110);
            foreach (string item in array)
            {
                list.Add(item);
            }
            
            return list;
        }

        /// <summary>
        /// 11排列3(990种组合)
        /// </summary>
        public static List<string> Get11A3()
        {
            string[] array = new string[990];
            int index = 0;
            for (int i = 1; i <= 9; i++)
            {
                for (int j = i + 1; j <= 10; j++)
                {
                    for (int k = j + 1; k <= 11; k++)
                    {
                        array[index++] = String.Format(FORMAT_N3, i, j, k);
                        array[index++] = String.Format(FORMAT_N3, i, k, j);

                        array[index++] = String.Format(FORMAT_N3, j, i, k);
                        array[index++] = String.Format(FORMAT_N3, j, k, i);

                        array[index++] = String.Format(FORMAT_N3, k, i, j);
                        array[index++] = String.Format(FORMAT_N3, k, j, i);
                    }
                }
            }
            Array.Sort(array);

            List<string> list = new List<string>(110);
            foreach (string item in array)
            {
                list.Add(item);
            }

            return list;
        }


        /// <summary>
        /// 一胆二拖买任选二(3420种组合)
        /// </summary>
        public static List<string> GetD1T2()
        {
            const string numbers = "01 02 03 04 05 06 07 08 09 10 11";

            List<string> list = new List<string>(495);

            for (int num = 1; num <= 11; num++)
            {
                string str = numbers.Replace(num.ToString("D2"), "").Replace("  ", " ").Trim();

                int[] array = ConvertStringToIntArray(str);
                for (int i = 0; i < 9; i++)
                {
                    for (int j = i + 1; j < 10; j++)
                    {
                        list.Add(String.Format(FORMAT_N3, num, array[i], array[j]));
                    }
                }
            }

            return list;
        }





        public static string Sort(string result)
        {
            object[] array = Array.ConvertAll(result.Split(' '), value => (object)value);

            Array.Sort<object>(array);

            switch (result.Split(' ').Length)
            {
                case 1: return String.Format(FORMAT_N1, array);
                case 2: return String.Format(FORMAT_N2, array);
                case 3: return String.Format(FORMAT_N3, array);
                case 4: return String.Format(FORMAT_N4, array);
                case 5: return String.Format(FORMAT_N5, array);
                case 6: return String.Format(FORMAT_N6, array);
                case 7: return String.Format(FORMAT_N7, array);
                case 8: return String.Format(FORMAT_N8, array);
                default: return result;
            }
        }

        public static int[] ConvertStringToIntArray(string value)
        {
            return Array.ConvertAll(value.Split(' '), val => Convert.ToInt32(val));
        }
    }
}
